package com.wr.dao.impl;

import com.wr.annotation.Autowired;
import com.wr.annotation.Repository;
import com.wr.dao.AccountDao;
import com.wr.pojo.Account;
import com.wr.utils.ConnectionUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author 王瑞
 * @descreption:
 * @date 2020/2/14  22:56
 */

@Repository
public class AccountDaoImpl implements AccountDao {

    @Autowired
   private ConnectionUtil connectionUtil;

    public void setConnectionUtil(ConnectionUtil connectionUtil) {
        this.connectionUtil = connectionUtil;
    }

    @Override
    public Account selectByCardNo(String cardNo) throws Exception {
        Connection connection = connectionUtil.getConnection();
        String sql = "select * from account where cardNo = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,cardNo);
        ResultSet resultSet = preparedStatement.executeQuery();
        Account account = new Account();
        while (resultSet.next()){
            String name = resultSet.getString("name");
            Integer money = resultSet.getInt("money");
            account.setName(name);
            account.setMoney(money);
            account.setCardno(cardNo);
        }


        return account;
    }

    @Override
    public int update(Account account)throws Exception {
        Connection connection = connectionUtil.getConnection();

        String sql = "update account set money = ? where cardNo = ?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,account.getMoney());
        preparedStatement.setString(2,account.getCardno());
        int i = preparedStatement.executeUpdate();

        return i;
    }


}
